<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<title>DRV_SPI_Initialize Function</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="generator" content="Doc-O-Matic" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <link rel="STYLESHEET" href="default.css" type="text/css" />

<script type="text/javascript" src="scripts.js"></script>
</head>
<body class="Element700" onload="onBodyLoadEx('frames.html', 'topic', '00501.html');" onmousedown="onBodyMouseDown();">

<!-- Begin Popups -->

<!-- End Popups -->

<!-- Begin Page Header -->
<div class="Element710" id="areafixed">
<div class="Element94">
<a href="00007.html" target="topic">Driver Libraries Help</a> &gt; <a href="00137.html" target="topic">SPI Driver Library Help</a> &gt; <a href="00138.html" target="topic">Library Interface</a> &gt; a) System Functions &gt; <a href="00501.html" target="topic">DRV_SPI_Initialize Function</a></div>
<div class="Element92">
<table width="100%" cellspacing="0" cellpadding="0">
<tr><td width="25%">
<div class="Element1">
MPLAB Harmony Core Help</div>
</td><td width="25%">
<div class="Element2">
<a href="contents.html" target="tocidx">Contents</a> | <a href="00007.html" target="topic">Home</a></div>
</td><td width="25%">
<div class="Element90">
<a href="00138.html" target="topic">Previous</a> | <a href="00138.html" target="topic">Up</a> | <a href="00505.html" target="topic">Next</a></div>
</td><td width="25%">
<div class="Element96">
<a href="mailto:docerrors@microchip.com&subject=MPLAB Harmony Documentation Feedback (Topic ID: DRV_SPI_Initialize@SYS_MODULE_INDEX@SYS_MODULE_INIT * const Topic Title: DRV_SPI_Initialize Function)&body=Thank you for your feedback! Please include a description of your feedback, and indicate whether you are reporting an an error in the documentation or an enhancement.">Documentation Feedback</a><br> <a href="http://support.microchip.com" target="_blank">Microchip Support</a></div>
</td></tr></table><div class="Element5">
DRV_SPI_Initialize Function</div>
</div>
</div>

<!-- End Page Header -->

<!-- Begin Client Area -->
<div class="Element720" id="areascroll">
<div class="Element721">

<!-- Begin Page Content -->
<a name="PageContent"></a><div class="Element58">
<a name="4465736372697074696F6E"></a><div class="Element11">
<div class="Element10">
<p class="Element10">
This routine initializes the SPI driver instance for the specified driver index, making it ready for clients to open and use it. The initialization data is specified by the init parameter. The initialization may fail if the number of driver objects allocated are insufficient or if the specified driver instance is already initialized. The driver instance index is independent of the SPI module ID. For example, driver instance 0 can be assigned to SPI2.</p></div>
</div>
<a name="43"></a><div class="Element99">
C</div>
<div class="Element101"><div class="Element100"><pre class="Element100"><a href="01189.html" target="topic">SYS_MODULE_OBJ</a> <strong><span style="color: #000000">DRV_SPI_Initialize</span></strong>(
    <strong><span style="color: #000080">const</span></strong> <a href="01183.html" target="topic">SYS_MODULE_INDEX</a> <strong><span style="color: #000000">index</span></strong>, 
    <strong><span style="color: #000080">const</span></strong> <a href="01184.html" target="topic">SYS_MODULE_INIT</a> * <strong><span style="color: #000080">const</span></strong> <strong><span style="color: #000000">init</span></strong>
);</pre></div></div>
<a name="507265636F6E646974696F6E73"></a><div class="Element14">
Preconditions</div>
<div class="Element11">
<div class="Element10">
<p class="Element10">
None.</p></div>
</div>
<a name="506172616D6574657273"></a><div class="Element14">
Parameters</div>
<div class="Element11">
<div class="Element10">
<div class="Element312">
<div class="TableDiv">
<table cellspacing="0" class="Table3">
<tr>
<td class="Element300" valign="top" width="35%">
<div class="Element301">
Parameters&nbsp;</div></td><td class="Element304" valign="top" width="65%">
<div class="Element305">
Description&nbsp;</div></td></tr><tr>
<td class="Element302" valign="top" width="35%">
<div class="Element303">
index&nbsp;</div></td><td class="Element306" valign="top" width="65%">
<div class="Element307">
Identifier for the instance to be initialized<br>&nbsp;</div></td></tr><tr>
<td class="Element302" valign="top" width="35%">
<div class="Element303">
init&nbsp;</div></td><td class="Element306" valign="top" width="65%">
<div class="Element307">
Pointer to the init data structure containing any data necessary to initialize the driver.&nbsp;</div></td></tr></table></div></div>
</div>
</div>
<a name="52657475726E73"></a><div class="Element14">
Returns</div>
<div class="Element11">
<div class="Element10">
<p class="Element10">
If successful, returns a valid handle to a driver instance object. Otherwise, returns <a href="01190.html" target="topic">SYS_MODULE_OBJ_INVALID</a>.</p></div>
</div>
<a name="52656D61726B73"></a><div class="Element14">
Remarks</div>
<div class="Element11">
<div class="Element10">

<ul class="Element630">
<li class="Element600">This routine must be called before any other SPI routine is called.</li>
<li class="Element600">This routine must only be called once during system initialization.</li>
<li class="Element600">This routine will NEVER block for hardware access.</li>
</ul></div>
</div>
<a name="4578616D706C65"></a><div class="Element14">
Example</div>
<div class="Element11">
<div class="Element10">
<div class="Element13"><div class="Element12"><pre class="Element12"><i><span style="color: #008000">// The following code snippet shows an example SPI driver initialization.</span></i>

<a href="01189.html" target="topic">SYS_MODULE_OBJ</a>   objectHandle;

<strong><span style="color: #000080">const</span></strong> DRV_SPI_PLIB_INTERFACE drvSPI0PlibAPI = {

    <i><span style="color: #008000">// SPI PLIB Setup</span></i>
    .setup = (DRV_SPI_PLIB_SETUP)SPI0_TransferSetup,

    <i><span style="color: #008000">// SPI PLIB WriteRead function</span></i>
    .writeRead = (DRV_SPI_PLIB_WRITE_READ)SPI0_WriteRead,

    <i><span style="color: #008000">// SPI PLIB Transfer Status function</span></i>
    .isBusy = (DRV_SPI_PLIB_IS_BUSY)SPI0_IsBusy,

    <i><span style="color: #008000">// SPI PLIB Callback Register</span></i>
    .callbackRegister = (DRV_SPI_PLIB_CALLBACK_REGISTER)SPI0_CallbackRegister,
};

<strong><span style="color: #000080">const</span></strong> DRV_SPI_INIT drvSPI0InitData = {

    <i><span style="color: #008000">// SPI PLIB API</span></i>
    .spiPlib = &amp;drvSPI0PlibAPI,

    .remapDataBits = drvSPI0remapDataBits,
    .remapClockPolarity = drvSPI0remapClockPolarity,
    .remapClockPhase = drvSPI0remapClockPhase,


    <i><span style="color: #008000">/// SPI Number of clients</span></i>
    .numClients = DRV_SPI_CLIENTS_NUMBER_IDX0,

    <i><span style="color: #008000">// SPI Client Objects Pool</span></i>
    .clientObjPool = (uintptr_t)&amp;drvSPI0ClientObjPool[0],

    <i><span style="color: #008000">// DMA Channel for Transmit</span></i>
    .dmaChannelTransmit = DRV_SPI_XMIT_DMA_CH_IDX0,

    <i><span style="color: #008000">// DMA Channel for Receive</span></i>
    .dmaChannelReceive  = DRV_SPI_RCV_DMA_CH_IDX0,

    <i><span style="color: #008000">// SPI Transmit Register</span></i>
    .spiTransmitAddress =  (<strong><span style="color: #000080">void</span></strong> *)&amp;(SPI0_REGS-&gt;SPI_TDR),

    <i><span style="color: #008000">// SPI Receive Register</span></i>
    .spiReceiveAddress  = (<strong><span style="color: #000080">void</span></strong> *)&amp;(SPI0_REGS-&gt;SPI_RDR),

    <i><span style="color: #008000">// Interrupt source is DMA</span></i>

   .interruptSource = XDMAC_IRQn,


    <i><span style="color: #008000">// SPI Queue Size</span></i>
    .queueSize = DRV_SPI_QUEUE_SIZE_IDX0,

    <i><span style="color: #008000">// SPI Transfer Objects Pool</span></i>
    .transferObjPool = (uintptr_t)&amp;drvSPI0TransferObjPool[0],
};

objectHandle = DRV_SPI_Initialize(DRV_SPI_INDEX_0,(<a href="01184.html" target="topic">SYS_MODULE_INIT</a>*)&amp;drvSPI0InitData);
<strong><span style="color: #000080">if</span></strong> (objectHandle == <a href="01190.html" target="topic">SYS_MODULE_OBJ_INVALID</a>)
{
    <i><span style="color: #008000">// Handle error</span></i>
}</pre></div></div>
</div>
</div>
</div>
<!-- End Page Content -->

<!-- Begin Page Footer -->
<div class="Element95">
<a href="00007.html" target="topic">Driver Libraries Help</a> &gt; <a href="00137.html" target="topic">SPI Driver Library Help</a> &gt; <a href="00138.html" target="topic">Library Interface</a> &gt; a) System Functions &gt; <a href="00501.html" target="topic">DRV_SPI_Initialize Function</a></div>
<div class="Element93">
<table width="100%" cellspacing="0" cellpadding="0">
<tr><td width="25%">
<div class="Element3">
MPLAB Harmony Core Help</div>
</td><td width="25%">
<div class="Element4">
<a href="contents.html" target="tocidx">Contents</a> | <a href="00007.html" target="topic">Home</a></div>
</td><td width="25%">
<div class="Element91">
<a href="00138.html" target="topic">Previous</a> | <a href="00138.html" target="topic">Up</a> | <a href="00505.html" target="topic">Next</a></div>
</td><td width="25%">
<div class="Element97">
<a href="mailto:docerrors@microchip.com&subject=MPLAB Harmony Documentation Feedback (Topic ID: DRV_SPI_Initialize@SYS_MODULE_INDEX@SYS_MODULE_INIT * const Topic Title: DRV_SPI_Initialize Function)&body=Thank you for your feedback! Please include a description of your feedback, and indicate whether you are reporting an an error in the documentation or an enhancement.">Documentation Feedback</a><br> <a href="http://support.microchip.com" target="_blank">Microchip Support</a></div>
</td></tr></table></div>

<!-- End Page Footer -->
</div>
</div>

<!-- End Client Area -->
</body></html>